1
'*************************** Module Header ********************************\
2 ' Module Name: Default.aspx.vb
3 ' Project: VBASPNETIPtoLocation
4 ' Copyright (c) Microsoft Corporation
6 ' This project illustrates how to get the geographical location from a db file.
7 ' You need install Sqlserver Express for run the web applicaiton. The code-sample
8 ' only support Internet Protocol version 4.
10 ' This source is subject to the Microsoft Public License.
11 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 ' All other rights reserved.
14 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '*****************************************************************************/
23 Imports System
.Data
.SqlClient
24 Imports System
.Configuration
27 Inherits System
.Web
.UI
.Page
29 Protected
Sub Page_Load(ByVal sender
As Object, ByVal e
As EventArgs
) Handles
Me.Load
30 Dim ipAddress
As String
32 ' Get the client's IP address.If you get the result of "::1", it's the IPv6 version
33 ' of your IP address, you can disable IPv6 components if you want to get IPv4 version.
34 ' And you need change the registry key when you disable it, check this link:
35 ipAddress
= Request
.ServerVariables("HTTP_X_FORWARDED_FOR")
36 If String.IsNullOrEmpty(ipAddress
) Then
37 ipAddress
= Request
.ServerVariables("REMOTE_ADDR")
39 lbIPAddress
.Text
= "You IP Address is: [" & ipAddress
& "]."
42 Protected
Sub btnSubmit_Click(ByVal sender
As Object, ByVal e
As EventArgs
) Handles btnSubmit
.Click
43 Dim ipAddress
As String = tbIPAddress
.Text
.Trim()
44 Dim locationInfo
As New Location()
45 If String.IsNullOrEmpty(ipAddress
) Then
46 Response
.Write("<strong>Please input an IP address</strong>")
50 ' Get the IP address string and calculate IP number.
51 Dim ipRange
As String = IPConvert
.ConvertToIPRange(ipAddress
)
52 Dim tabLocation
As New DataTable()
54 ' Create a connection to Sqlserver
55 Using sqlConnection
As New SqlConnection(ConfigurationManager
.ConnectionStrings("ConectString").ToString())
56 Dim selectCommand
As String = "select * from IPtoLocation where CAST(" & ipRange
& " as bigint) between BeginingIP and EndingIP"
57 Dim sqlAdapter
As New SqlDataAdapter(selectCommand
, sqlConnection
)
59 sqlAdapter
.Fill(tabLocation
)
62 ' Store IP infomation into Location entity class
63 If tabLocation
.Rows
.Count
= 1 Then
64 locationInfo
.BeginIP
= tabLocation
.Rows(0)(0).ToString()
65 locationInfo
.EndIP
= tabLocation
.Rows(0)(1).ToString()
66 locationInfo
.CountryTwoCode
= tabLocation
.Rows(0)(2).ToString()
67 locationInfo
.CountryThreeCode
= tabLocation
.Rows(0)(3).ToString()
68 locationInfo
.CountryName
= tabLocation
.Rows(0)(4).ToString()
70 Response
.Write("<strong>Cannot find the location based on the IP address [" & ipAddress
& "].</strong> ")
75 Response
.Write("<strong>Country Code(Two):</strong> ")
76 Response
.Write(locationInfo
.CountryTwoCode
+ "<br />")
78 Response
.Write("<strong>Country Code(Three):</strong> ")
79 Response
.Write(locationInfo
.CountryThreeCode
+ "<br />")
81 Response
.Write("<strong>Country Name:</strong> ")
82 Response
.Write(locationInfo
.CountryName
+ "<br />")
84 lbIPAddress
.Visible
= False